<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="productId">
      <el-input v-model="dataForm.productId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="productPic">
      <el-input v-model="dataForm.productPic" placeholder="商品图片"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="productPrice">
      <el-input v-model="dataForm.productPrice" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="商品数量" prop="productNum">
      <el-input v-model="dataForm.productNum" placeholder="商品数量"></el-input>
    </el-form-item>
    <el-form-item label="商品重量" prop="productWeight">
      <el-input v-model="dataForm.productWeight" placeholder="商品重量"></el-input>
    </el-form-item>
    <!-- <el-form-item label="价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="价格"></el-input>
    </el-form-item> -->
    <el-form-item label="运费" prop="cartSendPrice">
      <el-input v-model="dataForm.cartSendPrice" placeholder="运费"></el-input>
    </el-form-item>
    <!-- <el-form-item label="总价格" prop="totalPrice">
      <el-input v-model="dataForm.totalPrice" placeholder="总价格"></el-input>
    </el-form-item> -->
    <el-form-item label="1添加购物车已付款 2 为付款" prop="status">
      <el-input v-model="dataForm.status" placeholder="1添加购物车已付款 2 为付款"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <!-- <el-form-item label="修改时间" prop="modifyTime">
      <el-input v-model="dataForm.modifyTime" placeholder="修改时间"></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          productId: '',
          productName: '',
          productPic: '',
          productPrice: '',
          productNum: '',
          productWeight: '',
          price: '',
          cartSendPrice: '',
          totalPrice: '',
          status: '',
          createTime: '',
          modifyTime: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          productId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          productName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          productPic: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ],
          productPrice: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          productNum: [
            { required: true, message: '商品数量不能为空', trigger: 'blur' }
          ],
          productWeight: [
            { required: true, message: '商品重量不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          cartSendPrice: [
            { required: true, message: '运费不能为空', trigger: 'blur' }
          ],
          totalPrice: [
            { required: true, message: '总价格不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '1添加购物车已付款 2 为付款不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          modifyTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/cart/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.cart.userId
                this.dataForm.productId = data.cart.productId
                this.dataForm.productName = data.cart.productName
                this.dataForm.productPic = data.cart.productPic
                this.dataForm.productPrice = data.cart.productPrice
                this.dataForm.productNum = data.cart.productNum
                this.dataForm.productWeight = data.cart.productWeight
                this.dataForm.price = data.cart.price
                this.dataForm.cartSendPrice = data.cart.cartSendPrice
                this.dataForm.totalPrice = data.cart.totalPrice
                this.dataForm.status = data.cart.status
                this.dataForm.createTime = data.cart.createTime
                this.dataForm.modifyTime = data.cart.modifyTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cart/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'productId': this.dataForm.productId,
                'productName': this.dataForm.productName,
                'productPic': this.dataForm.productPic,
                'productPrice': this.dataForm.productPrice,
                'productNum': this.dataForm.productNum,
                'productWeight': this.dataForm.productWeight,
                'price': this.dataForm.price,
                'cartSendPrice': this.dataForm.cartSendPrice,
                'totalPrice': this.dataForm.totalPrice,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'modifyTime': this.dataForm.modifyTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
